package com.ruoyi.airspaceFlight.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.airspaceFlight.domain.TAirspaceSettings;
import com.ruoyi.airspaceFlight.mapper.TAirspaceSettingsMapper;
import com.ruoyi.common.core.utils.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Slf4j
@Component("StatusSchedule")
@RequiredArgsConstructor
public class StatusSchedule {

    @Autowired
    private TAirspaceSettingsMapper tAirspaceSettingsMapper;

    /**
     * @describe 空域设置表status定时更新
     * @params []
     * @author SUNLIANG
     * @date 2023/11/7
     */
    @Scheduled(cron = "0 0 0 * * ?", zone = "Asia/Shanghai") // 每天零点执行 默认使用服务器默认时区
    @Transactional(rollbackFor = Exception.class)
    public void status() {

        List<TAirspaceSettings> tAirspaceSettings = tAirspaceSettingsMapper.selectList(new LambdaQueryWrapper<TAirspaceSettings>().ne(TAirspaceSettings::getStatus,2));

        for (TAirspaceSettings tAirspaceSetting : tAirspaceSettings) {
            System.out.println(tAirspaceSetting);
            //0-生效中 1-待生效 2-已停用 3-已到期
            if (tAirspaceSetting.getStartTime().after(DateUtils.getNowDate())) {
                tAirspaceSetting.setStatus(1);
                tAirspaceSettingsMapper.updateById(tAirspaceSetting);
            } else if (tAirspaceSetting.getEndTime().before(DateUtils.getNowDate())) {
                tAirspaceSetting.setStatus(3);
                tAirspaceSettingsMapper.updateById(tAirspaceSetting);
            } else if (tAirspaceSetting.getStartTime().before(DateUtils.getNowDate()) && tAirspaceSetting.getEndTime().after(DateUtils.getNowDate())) {
                tAirspaceSetting.setStatus(0);
                tAirspaceSettingsMapper.updateById(tAirspaceSetting);
            } else if (tAirspaceSetting.getStartTime().equals(DateUtils.getNowDate()) || tAirspaceSetting.getEndTime().equals(DateUtils.getNowDate())){
                tAirspaceSetting.setStatus(0);
                tAirspaceSettingsMapper.updateById(tAirspaceSetting);
            }
        }

    }


}
